package com.huihui.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.huihui.R;
import com.huihui.activity.pb.PublicActivity;
import com.huihui.utils.IntentUtils;
import com.huihui.utils.LoadImageUtil;
import com.huihui.utils.StringUtils;
import com.huihui.utils.ToolUtils;

import java.io.File;
import java.util.List;

/**
 * Created by Administrator on 2018/8/18.
 */

public class MineZanGridViewAdapter extends BaseAdapter {

    private Context context;
    private List<String> list;
    private LayoutInflater inflater;


    public MineZanGridViewAdapter(List<String> listUrls,Context context) {
        this.list = listUrls;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public int getCount(){
        return  list == null ? 0: list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Nullable
    @Override
    public CharSequence[] getAutofillOptions() {
        return new CharSequence[0];
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_image, parent, false);
            holder.image = (ImageView) convertView.findViewById(R.id.imageView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ViewGroup.LayoutParams layoutParams = holder.image.getLayoutParams();
        int width = ToolUtils.getWidth(context);
        int imageViewWidth = (width - ToolUtils.dp2px(context,43))/3;
        layoutParams.width = imageViewWidth;
        layoutParams.height = imageViewWidth;
        holder.image.setLayoutParams(layoutParams);
        final String path = list.get(position);
        if(StringUtils.isGif(path)){
            LoadImageUtil.displayImage(context, path, holder.image);
        }else {
            LoadImageUtil.displayJuxingImage(context, path, holder.image);
        }

        holder.image.setTag(position);
        holder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentUtils.gotoPreviewActivity(context,list,position);
            }
        });

        return convertView;
    }

    class ViewHolder {
        ImageView image;
    }
}


